
/* GCSx
** SCRIPTEDIT.H
**
** Script support
** Expands basic Script to include editor functionality
*/

/*****************************************************************************
** Copyright (C) 2003-2006 Janson
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
** 
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
*****************************************************************************/

#ifndef __GCSx_SCRIPTEDIT_H_
#define __GCSx_SCRIPTEDIT_H_

class ScriptEdit : public Script, virtual public SaveLoad {
protected:
    // Has the script been modified from initial state (from empty if new)
    int headerModified;
    int contentModified;
    int disassociated;
    int inMiddleOfUndoBlock;

    int dependencyModified; // Triggers recompile
    int sourceModified; // Triggers reparse/recompile

    // Our node on the world browser
    TreeView* browserNode;

    // Set us as modified
    // Setting content modified also loads from cache if needed
    // Call these before actually making any modifications
    // (if header changes affect content, call both modifieds first)
    void setHeaderModified();
    void setContentModified();

    // must be uncached
    void clean(int full = 1);

public:
    // Id of 0 = we're going to load (or create/import) immediately
    // Any other id = we start out modified
    // World and ID may be NULL/0 if in process of creating
    ScriptEdit(class WorldEdit* myWorld, Script::ScriptType type = Script::SCRIPT_CODE, int myId = 0); // Starts with default settings
    virtual ~ScriptEdit();
    
    // Intended only for use after creating/importing- not in general usage
    void setInfo(class WorldEdit* myWorld, int newId);
    
    // Tells us to disassociate ourselves from anything, we've been "deleted"
    // or that we've been "readded" again
    // FileException means couldn't decache, and nothing was done
    void disassociate() throw_File;
    // Node is world browser node that we readd to
    void reassociate(TreeView* node);

    class WorldEdit* getWorldEdit() { return dynamic_cast<WorldEdit*>(world); }
    const class WorldEdit* getWorldEdit() const { return dynamic_cast<WorldEdit*>(world); }
    
    // Change properties; handles undo
    void setName(const std::string& newName, Window* srcWin = NULL, Window* exWin = NULL) throw_Undo;
    void setDefault(class AnimGroup* newAnimgroup, class TileSet* newTileset, int newId, Window* srcWin = NULL, Window* exWin = NULL) throw_Undo;
    std::list<std::string>& getSource(); // Must LOCK first
    
    // Prepares for needed or forced compilation (lock optional)
    void cleanForCompile(int force = 0);
    
    // Must LOCK first
    // These only compile as needed- calls cleanForCompile for you
    // Errors may result
    void parseFuncs();
    void compile(); // Includes parse if not done
    void link();
    void decompile();
    
    // Returns true if OK pressed
    // Locks and unlocks script
    int propertiesDialog(Window* srcWin = NULL, Window* exWin = NULL);
    
    // Code window (editor)
    void openEditWindow();
    
    // Tells script it's code has been modified (by an edit window, etc.)
    enum ContentChangeType {
        SCRIPT_REMOVE_LINES = 1,
        SCRIPT_INSERT_LINES, // It's assumed these are modified as well
        SCRIPT_MODIFY_LINE, // One or more lines
        SCRIPT_MODIFY_DONE, // Called after each batch is complete
    };
    // Call BEFORE making changes, so it can properly save undo
    void codeModifiedPre(ContentChangeType type, int firstRow, int numRows, EditBox* editbox = NULL, Window* srcWin = NULL);
    // Call AFTER making changes, so it can properly send notification
    void codeModifiedPost(ContentChangeType type, int firstRow, int numRows, EditBox* editbox = NULL, Window* exWin = NULL);

    // Tells script to add itself to the world browser node
    void addToBrowser(TreeView* node, int makeCurrent = 1);
    void dropBrowser(); // Called if script gets dropped from browser

    // Treeview event handling
    int treeviewEvent(int code, int command, int check);
    static int treeviewEventWrap(void* ptr, int code, int command, int check);
    
    // File access
    int isHeaderModified() const;
    int isContentModified() const;
    Uint32 saveHeader(FileWrite* file) throw_File;
    void saveContent(FileWrite* file) throw_File;
    void saveSuccess();
    void cachedContent(FileRead* file, int oldData);
};

#endif
